package com.msb.search.config;

import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * @Author: lmf
 * @Create: 2025/3/19 20:52
 * @module
 */
//@Configuration
//public class ResHighLeveClientConfig {
//    @Value("#{'${elasticsearch.hostAndPorts}'.split(',')}")
//    private List<String> hostAndPorts;
//
//    @Value("${elasticsearch.username}")
//    private String username;
//
//    @Value("${elasticsearch.password}")
//    private String password;
//
//    /**
//     * 创建RestHighLevelClient
//     */
////    @Bean
//
//    public RestHighLevelClient restHighLevelClient() {
//
//        final HttpHost[] httpHosts = new HttpHost[hostAndPorts.size()];
//        for (int i = 0; i < hostAndPorts.size(); i++) {
//            String[] hostAndPort = hostAndPorts.get(i).split(":");
//            httpHosts[i] = new HttpHost(hostAndPort[0], Integer.parseInt(hostAndPort[1]));
//        }
//
//        // 设置认证信息
//        final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
//        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("elastic", "aA9527!!"));
//
//        // 构建时设置连接信息，基于set设置认证信息
//        RestClientBuilder restClientBuilder = RestClient.builder(httpHosts);
//        restClientBuilder.setHttpClientConfigCallback(httpAsyncClientBuilder -> httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider));
//
//        return new RestHighLevelClient(restClientBuilder);
//    }
//}
